/*
 * Copyright 2020-2099 sa-token.cc
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.mjk.manager.satoken.starter.spring.autoware;

import cn.dev33.satoken.config.SaTokenConfig;
import cn.dev33.satoken.json.SaJsonTemplate;
import cn.dev33.satoken.json.SaJsonTemplateDefaultImpl;
import com.mjk.common.util.ObjectUtils;
import com.mjk.manager.satoken.starter.spring.json.SaJsonTemplateForGson;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 注册Sa-Token所需要的Bean
 * <p> Bean 的注册与注入应该分开在两个文件中，否则在某些场景下会造成循环依赖
 *
 * @author click33
 */
@Configuration
public class SaBeanRegister {

    @Value("${jwt.secret.key:bsdasdasiahueuifyurfewbfjsdafjk}")
    String jwtSecretKey;

    /**
     * 获取配置Bean
     *
     * @return 配置对象
     */
    @Bean
    @ConfigurationProperties(prefix = "sa-token")
    public SaTokenConfig getSaTokenConfig() {
        SaTokenConfig config = new SaTokenConfig();
        if (ObjectUtils.isEmpty(config.getJwtSecretKey())) {
            config.setJwtSecretKey(jwtSecretKey);
        }
        return config;
    }

    /**
     * 获取 json 转换器 Bean (Jackson版)
     *
     * @return json 转换器 Bean (Jackson版)
     */
    @Bean
    public SaJsonTemplate getSaJsonTemplateForJackson() {
        return new SaJsonTemplateForGson();
    }


}
